//
//  KLToBeshopViewController.m
//  KunLun
//
//  Created by Mac on 2017/11/28.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLToBeshopViewController.h"
#import "KLShopMenuControl.h"
#import "STRadioButton.h"
#import "STTextView.h"
#import "UITableView+STKeybordAdjust.h"
#import "KLChannelViewController.h"
#define XInsetValeu 20
#define nomalHeight 44
@interface KLToBeshopViewController ()
@property(nonatomic, strong) STRadioButton                     *radioButton;

@property(nonatomic, strong) UITextField                     *shopNameTextfiled;
@property(nonatomic, strong) UITextField                     *idTextfiled;
@property(nonatomic, strong) UITextField                     *realNameTextfiled;
@property(nonatomic, strong) STRadioButton                     *typeRadioButton;

@property(nonatomic, strong) STButton                        *addressButton;
@property(nonatomic, strong) NSString                     *codeString;
@property(nonatomic, strong) NSString                     *placesString;
@property(nonatomic, strong) UITextField                     *detailTextfiled;
@property(nonatomic, strong) STTextView                     *destextView;
@property(nonatomic, strong) STButton                     *addImageButton;
@property(nonatomic, strong) UIImage                      *originmage ;
@end

@implementation KLToBeshopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"申请店铺";
    self.originmage = [UIImage imageNamed:@"sz_xiangce"];
    [self configSubView];
    [self.tableView st_autoAdjustAllResponder];
   
}

#pragma mark --subView
- (void)configSubView{
    [self configHeaderView];
    [self configFooterView];
}
- (UIView*)headerViewWithTitle:(NSString*)title{
    UIView * backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, nomalHeight)];
    backView.backgroundColor = [UIColor whiteColor];
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(XInsetValeu, 0, UIScreenWidth - 2* XInsetValeu, nomalHeight)
                                                     text:title
                                                textColor:KL_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [backView addSubview:titleLable];
    return backView;
}
- (void)configHeaderView{
    
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 150)];
    header.backgroundColor = [UIColor whiteColor];
    
//    UIView * yusuView = [self headerViewWithTitle:@"是否要成为匀速会员"];
//    self.radioButton = [[STRadioButton alloc] initWithRadio:@[@"否",@"是"]
//                                               andWithFrame:CGRectMake(XInsetValeu, yusuView.bottom, 120, 40)];
//    self.radioButton.makenumOfarrayChecked = 0;
//    [header addSubview:yusuView];
//    [header addSubview:self.radioButton];
    
    UIView * shopNameView = [self headerViewWithTitle:@"店铺名称:"];
    shopNameView.top = 0;
    self.shopNameTextfiled = [[UITextField alloc] initWithFrame:CGRectMake(XInsetValeu, shopNameView.bottom, UIScreenWidth - 2*XInsetValeu, 30)];
    self.shopNameTextfiled.placeholder = @"请输入店铺名";
    self.shopNameTextfiled.clearButtonMode = UITextFieldViewModeWhileEditing;
    [header addSubview:shopNameView];

    UIView * line1 = [[UIView alloc] initWithFrame:CGRectMake(0, self.shopNameTextfiled.bottom, UIScreenWidth, 0.5)];
    line1.backgroundColor = KL_lineColor;
    [header addSubview:line1];
    [header addSubview:self.shopNameTextfiled];
    
    //匀加速id
//    UIView * idView = [self headerViewWithTitle:@"匀加速结算编号:"];
//    idView.top = line1.bottom;
//    self.idTextfiled = [[UITextField alloc] initWithFrame:CGRectMake(XInsetValeu, idView.bottom, UIScreenWidth - 2*XInsetValeu, 30)];
//    self.idTextfiled.placeholder = @"匀加速结算编号";
//    self.idTextfiled.clearButtonMode = UITextFieldViewModeWhileEditing;
//    self.idTextfiled.keyboardType = UIKeyboardTypeNumberPad;
//    [header addSubview:idView];
//    [header addSubview:self.idTextfiled];
//    UIView * line2 = [[UIView alloc] initWithFrame:CGRectMake(0, self.idTextfiled.bottom, UIScreenWidth, 0.5)];
//    line2.backgroundColor = KL_lineColor;
//    [header addSubview:line2];
    //真实姓名
    UIView * realNameView = [self headerViewWithTitle:@"真实姓名:"];
    realNameView.top = line1.bottom;
    self.realNameTextfiled = [[UITextField alloc] initWithFrame:CGRectMake(XInsetValeu, realNameView.bottom, UIScreenWidth - 2*XInsetValeu, 30)];
    self.realNameTextfiled.placeholder = @"请输入真实姓名";
    self.realNameTextfiled.clearButtonMode = UITextFieldViewModeWhileEditing;
    [header addSubview:realNameView];
    [header addSubview:self.realNameTextfiled];
    UIView * line3 = [[UIView alloc] initWithFrame:CGRectMake(0, self.realNameTextfiled.bottom, UIScreenWidth, 0.5)];
    line3.backgroundColor = KL_lineColor;
    [header addSubview:line3];
    //店铺类型
//    UIView * shopTypeView = [self headerViewWithTitle:@"选择店铺发布类型"];
//    shopTypeView.top = self.realNameTextfiled.bottom;
//    [header addSubview:shopTypeView];
   
//    self.typeRadioButton = [[STRadioButton alloc] initWithRadio:@[@"各地特产",@"名车出租",@"旅游住宿",@"菜篮子"] andItemSize:CGSizeMake(110, 44) andWithFrame: CGRectMake(XInsetValeu, shopTypeView.bottom, UIScreenWidth - 2*XInsetValeu, 30)];
//    self.typeRadioButton.makenumOfarrayChecked = 0;
//    [header addSubview:self.typeRadioButton];

    
    __weak typeof(self) weakSelf =  self;
    STButton * ruleButton = [[STButton alloc] initWithFrame:CGRectMake(XInsetValeu, line3.bottom, UIScreenWidth - 20, 40)
                                                      title:@"平台服务规则"
                                                 titleColor:KL_firstTextColor
                                                  titleFont:15
                                               cornerRadius:0
                                            backgroundColor:[UIColor whiteColor]
                                            backgroundImage:nil
                                                      image:[UIImage imageNamed:@"问号"]];
    [ruleButton makeImageRight];
    [ruleButton setClicAction:^(UIButton *sender) {
        [weakSelf onsSelectedRultButton];
    }];
    [header addSubview:ruleButton];
    
    //经营地址
    self.addressButton = [[STButton alloc] initWithFrame:CGRectMake(XInsetValeu, ruleButton.bottom, UIScreenWidth - 2*XInsetValeu, 40)
                                                   title:@"点击修改经营地址"
                                              titleColor:KL_firstTextColor
                                               titleFont:15
                                            cornerRadius:0
                                         backgroundColor:nil
                                         backgroundImage:nil
                                                   image:nil];
    [self.addressButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedAddressButton];
    }];
    [header addSubview:self.addressButton];
    
    //详细地址
    UIView * detailAdressView = [self headerViewWithTitle:@"详细地址"];
    detailAdressView.top = self.addressButton.bottom;
    [header addSubview:detailAdressView];
    self.detailTextfiled = [[UITextField alloc] initWithFrame:CGRectMake(XInsetValeu, detailAdressView.bottom, UIScreenWidth - 2*XInsetValeu, 30)];
    self.detailTextfiled.placeholder = @"请输入详细地址";
    [header addSubview:self.detailTextfiled];
    UIView * line4 = [[UIView alloc] initWithFrame:CGRectMake(0, self.detailTextfiled.bottom, UIScreenWidth, 0.5)];
    line4.backgroundColor = KL_lineColor;
    [header addSubview:line4];
    //点铺简介
    UIView * desView = [self headerViewWithTitle:@"店铺简介"];
    desView.top = line4.bottom;
    [header addSubview:desView];
    self.destextView = [[STTextView alloc] initWithFrame:CGRectMake(XInsetValeu, desView.bottom, UIScreenWidth - 2*XInsetValeu, 60)];
    self.destextView.placeholder = @"请输入店铺简介";
    [self.destextView st_setBorderWith:1 borderColor:KL_lineColor cornerRadius:5];
    [header addSubview:self.destextView];
    
    header.height = self.destextView.bottom;
    self.tableView.tableHeaderView = header;
}
- (void)configFooterView{
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = [UIColor whiteColor];
    
    UIView * imageView = [self headerViewWithTitle:@"上传店铺图片"];
    [footer addSubview:imageView];
    self.addImageButton = [[STButton alloc] initWithFrame:CGRectMake(XInsetValeu, imageView.bottom,100 , 80)
                                                  title:@""
                                             titleColor:KL_thirdTextColor
                                              titleFont:14
                                           cornerRadius:0
                                        backgroundColor:KL_backgroundColor
                                        backgroundImage:nil
                                                  image:_originmage];
    self.addImageButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.addImageButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedAddImageButton];
    }];
    [footer addSubview:self.addImageButton];
    
//    UIView * ideView = [self headerViewWithTitle:@"平台服务费支付方式:"];
//    ideView.top = self.addImageButton.bottom + 10;
//    [footer addSubview:ideView];
//    KLShopMenuControl * ideControl = [[KLShopMenuControl alloc]initWithFrame:CGRectMake(0, ideView.bottom, UIScreenWidth, 60) titles:@[@"1天",@"10天",@"20天",@"1个月"] subTitles:@[@"9元",@"90元",@"180元",@"240元"]  subColor:KL_firstTextColor];
//    ideControl.canMutipleSelected = NO;
//    [footer addSubview:ideControl];
    

    
    
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, self.addImageButton.bottom + 40, UIScreenWidth - 60, 44)
                                                        title:@"立即申请"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:10
                                              backgroundColor:KL_BlueBackGroundColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedCommitButton];
    }];
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:confimButton];
    
    footer.height = confimButton.bottom + 20;
    self.tableView.tableFooterView = footer;
}
#pragma mark --Action Method
//规则
- (void)onsSelectedRultButton{
    
}
//诚信金
- (void)onsSelectedideButton{
    
}
- (void)onSelectedAddressButton{
    
    KLChannelViewController * vc = [KLChannelViewController new];
    [vc setDidChoseArea:^(NSString *places, NSString *codes) {
        [self.addressButton setTitle:places forState:UIControlStateNormal];
        self.codeString = codes;
        places = [places stringByReplacingOccurrencesOfString:@"-" withString:@""];
        self.placesString = places;
    }];
    [self.navigationController pushViewController:vc animated:YES];
}
//申请
- (void)onSelectedCommitButton{
    if (!self.shopNameTextfiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入店铺名称"];
        return;
    }
    if (!self.idTextfiled.text.length && self.radioButton.checkedNum.integerValue == 1) {
        [SVProgressHUD showErrorWithStatus:@"请输入匀加速id"];
        return;
    }
    if (!self.realNameTextfiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入真实姓名"];
        return;
    }
    if ([self.addressButton.currentTitle isEqualToString:@"点击修改经营地址"]) {
        [SVProgressHUD showErrorWithStatus:@"请输入经营地址"];
        return;
    }
    if (!self.detailTextfiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入详细地址"];
        return;
    }
    
    if (!self.destextView.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入店铺简介"];
        return;
    }
    if (self.addImageButton.currentImage == self.originmage) {
        [SVProgressHUD showErrorWithStatus:@"请选择店铺图片"];
        return;
    }
    [SVProgressHUD showWithStatus:@"申请中"];
    [KLUtils sendQiNiuWithImage:self.addImageButton.currentImage handle:^(NSString *imageUrl, NSString *errorDes) {
        [SVProgressHUD dismiss];
        if (imageUrl.length) {
            [self sendTobeShopRequestWithImageUrl:imageUrl];
        }else{
            [SVProgressHUD showErrorWithStatus:@"上传图片失败"];
        }
        
    }];
}
- (void)onSelectedAddImageButton{
    __weak typeof(self) weakSelf =  self;
    [self showDefultImagePicker:^(UIImage *image) {
        [weakSelf.addImageButton setImage:image forState:UIControlStateNormal];
    }];
}
#pragma mark --NetWork Method
- (void)sendTobeShopRequestWithImageUrl:(NSString*)imageUrl{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@%@",serviceUrl,@"/api/shop/addshop",[NSString stringWithFormat:@"?token=%@",[STUserManger defult].token]];
    
    if (self.shopNameTextfiled.text.length) {
        [dic setObject:self.shopNameTextfiled.text forKey:@"shops_name"];
    }
    if (self.idTextfiled.text.length && self.radioButton.checkedNum.integerValue == 1) {
        [dic setObject:self.idTextfiled.text forKey:@"shops_no"];
    }
    if (self.realNameTextfiled.text.length) {
        [dic setObject:self.realNameTextfiled.text forKey:@"name"];
    }
    if (self.codeString) {
        NSArray * array = [self.codeString componentsSeparatedByString:@"-"];
        
         NSString * province = array.firstObject;
        [dic setObject:province forKey:@"province"];
         NSString * city = array[1];
         [dic setObject:city forKey:@"city"];
        if (array.count > 2) {
             NSString * county = array[2];
             [dic setObject:county forKey:@"county"];
        }
        if (array.count > 3) {
            NSString * town = array[3];
             [dic setObject:town forKey:@"town"];
        }
        
    }
    if (self.detailTextfiled.text.length) {
        NSString * add = self.detailTextfiled.text;
        if (self.placesString.length) {
            add = [NSString stringWithFormat:@"%@%@",self.placesString,add];
        }
        [dic setObject:add forKey:@"address"];
    }
    if (self.destextView.text.length) {
        [dic setObject:self.destextView.text forKey:@"abstract"];
    }
    if (imageUrl.length) {
        [dic setObject:imageUrl forKey:@"head_portrait"];
    }
    [dic setObject:@"0" forKey:@"is_associator"];

    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"申请成功"];
                                                 [self.navigationController popViewControllerAnimated:YES];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end

